<?php

class ComplaintController extends BaseController {
	
	public function index() {
		$salesSvc = new SalesService();
		
		$result = $salesSvc->getTodaySales();
		
		$this->view->setDisplay('sales', $result);
		
		$this->view->setJavascript(BaliwagConstant::JQUERY_LIB);
		$this->view->setJavascript(BaliwagConstant::JQUERY_UI_JS_LIB);
		$this->view->setJavascript('main');
		$this->view->setJavascript('complaint');
		
		$this->view->setCss('main');
		$this->view->setCss('employee');
		$this->view->setCss('ribbon');
		$this->view->setCss('products');
		$this->view->setCss('complaint');
		$this->view->setCss(BaliwagConstant::JQUERY_UI_CSS_LIB);
		
		$this->view->setTitle(BaliwagConstant::PAGE_TITLE_WITH_DASH . 'Complaint');
		
		$this->view->template('pages/complaint/complaint');
	}
	
	public function getSalesInfo() {
		$sales_id = $this->post('sales_id');
		
		$salesSvc = new SalesService();
		
		$result = $salesSvc->getSalesDetails($sales_id);
		
		$data['sales_id'] = $result->getSalesId();

		foreach ($result->getSalesDetails() AS $salesDetails) {
			$d['sales_details_id'] = $salesDetails->getSalesDetailId();
			$d['sales_details_total_count'] = $salesDetails->getProductQty();
			$d['sales_details_total_price'] = $salesDetails->getTotalPrice();
			$d['sales_details_product_name'] = $salesDetails->getProduct()->getProductName();
			
			$data['sales_details'][] = $d;
		}

		echo json_encode($data);
	}
	
	public function getSalesDetail() {
		$sales_detail_id = $this->post('sales_detail_id');
		
		$salesSvc = new SalesService();
		
		$result = $salesSvc->getSalesDetail($sales_detail_id);
		
		$data['sales_detail_id'] = $result[0]->getSalesDetailId();
		$data['sales_detail_qty'] = $result[0]->getProductQty();
		$data['product_name'] = $result[0]->getProduct()->getProductName();
		$data['product_qty'] = $result[0]->getProduct()->getProductQty();
		$data['product_price'] = $result[0]->getProduct()->getProductPrice();
		
		echo json_encode($data);
	}
	
	public function saveComplaint() {
		$sales_detail_id = $this->post('sales_detail_id');
		$employee = $_SESSION['employee'];
		$product_count = $this->post('product_count');
		$product_price = $this->post('product_price');
		$reason = $this->post('reason');
		
		$complaintSvc = new ComplaintService();
		
		$complaintSvc->addComplaint($sales_detail_id, $employee, $product_count, $product_price, $reason);
	}
}